import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/util/dao_util.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 点击回调方法
typedef RecOnTapFun = void Function(BuildContext context, String type);

/// 试卷组件
class RecommendItemWidget extends StatelessWidget {
  /// 标题
  final String title;

  /// 人数（观看/做过）
  final int num;

  /// 副标题，副的相关数据
  final String subtitle;

  /// 试卷/资料的对应格式
  final String type;

  /// 显示模式，true：试卷状态，false：资料显示
  final bool showPaper;

  /// 右边按钮颜色显示，true：蓝底白字，false：黄底棕字
  final bool btnType;

  /// 右边按钮显示字符串
  final String btnStr;

  /// 点击方法
  final RecOnTapFun onTapFun;

  /// 资料对应路径，资料模式时才需要传递
  final String resUrl;

  const RecommendItemWidget({
    super.key,
    required this.title,
    required this.num,
    required this.subtitle,
    required this.type,
    required this.showPaper,
    required this.btnType,
    required this.btnStr,
    required this.onTapFun,
    this.resUrl = "",
  });

  @override
  Widget build(BuildContext context) {
    // debugPrint("resourceItemWidget 组件 -> build 方法实现 ===> ");
    debugPrint("");
    return SizedBox(
      height: 120,
      child: Row(
        children: [
          _iconItem(),
          hiSpace(width: 24),
          Expanded(child: _body()),
          hiSpace(width: 24),
          _btnItem(context),
        ],
      ),
    );
  }

  /// 图标显示
  Widget _iconItem() {
    return showPaper ? _paperIconItem() : _recIconItem();
  }

  /// 资料相关图标显示
  Widget _recIconItem() {
    String url = "";
    if (type == "pdf") {
      url = "images/home/information_pdf.svg";
    } else if (type == "doc" || type == "docx") {
      url = "images/home/information_word.svg";
    } else if (type == "txt") {
      url = "images/home/information_txt.svg";
    } else if (type == "xls" || type == "xlsx") {
      url = "images/home/information_exl.svg";
    } else if (type == "ppt" || type == "pptx") {
      url = "images/home/information_ppt.svg";
    } else if (type == "png" || type == "jpg" || type == "jpeg") {
      String url = coverHead + resUrl;
      return SizedBox(
        width: 88,
        height: 112,
        child: Image.network(
          url,
          width: 88,
          height: 112,
          fit: BoxFit.fill,
        ),
      );
    }
    if (url == "") {
      return SizedBox(
        width: 88,
        height: 112,
      );
    }
    return SvgPicture.asset(
      url,
      width: 33,
      height: 42,
    );
  }

  /// 试卷图标显示
  Widget _paperIconItem() {
    return SizedBox(
      width: 88,
      height: 112,
      child: Stack(
        children: [
          Positioned.fill(
            child: SvgPicture.asset(
              "images/home/paper_dad.svg",
              width: 88,
              height: 112,
            ),
          ),
          Positioned(
            top: 58,
            left: 20,
            child: Text(
              type,
              style: FontUtil.fontOrange13(
                24,
                // fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 显示主体
  Widget _body() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      spacing: 8,
      children: [
        Text(
          title,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: FontUtil.fontBlack1(18, fontWeight: FontWeight.w500),
        ),
        if (showPaper) _subheading(),
        if (showPaper) hiSpace(height: 6),
        Text(
          "$num 人${showPaper ? "做" : "看"}过",
          style: FontUtil.fontGrey1(18),
        )
      ],
    );
  }

  /// 标题下面的显示方法
  _subheading() {
    String str = "";
    if (showPaper) {
      str = "总题数 $subtitle";
    } else {
      str = subtitle;
    }
    return Text(
      str,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      style: FontUtil.fontGrey2(24),
    );
  }

  /// 按钮组件
  Widget _btnItem(BuildContext context) {
    return GestureDetector(
      onTap: () {
        onTapFun(context, type);
      },
      child: _button(),
    );
  }

  /// 按钮样式
  _button() {
    return Container(
      width: 108,
      decoration: BoxDecoration(
          color: btnType ? colorBlue1 : colorYellow1,
          borderRadius: BorderRadius.all(Radius.circular(24))),
      padding: EdgeInsets.only(top: 5,bottom: 8),
      child: Text(
        btnStr,
        style: btnType ? FontUtil.fontWhile1(18) : FontUtil.fontBrown1(18),
        textAlign: TextAlign.center,
      ),
    );
  }
}
